﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using agsXMPP;
using agsXMPP.ui;
using agsXMPP.protocol.iq.roster;

namespace MyClient
{
    public partial class MainForm : Form
    {
        private XmppClientConnection xmppCon = new XmppClientConnection();

        public MainForm()
        {
            InitializeComponent();

            loginToolStripMenuItem.Enabled = true;
            logoutToolStripMenuItem.Enabled = false;
        }

        void xmppCon_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new agsXMPP.XmppClientConnection.RosterHandler(xmppCon_OnRosterItem), new object[] { this, item });
                return;
            }

            if (item.Subscription != SubscriptionType.remove)
            {
                rosterControl.AddRosterItem(item);
            }
            else
            {
                rosterControl.RemoveRosterItem(item);
            }	
        }

        void xmppCon_OnRosterEnd(object sender)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ObjectHandler(xmppCon_OnRosterEnd), new object[]{this});
                return;
            }
            rosterControl.ExpandAll();
        }

        void xmppCon_OnRosterStart(object sender)
        {
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoginForm login_form = new LoginForm(xmppCon);
            DialogResult d_result = login_form.ShowDialog();
            if (d_result==DialogResult.OK)
            {
                xmppCon = login_form.xmppConn;
                xmppCon.OnRosterStart += new ObjectHandler(xmppCon_OnRosterStart);
                xmppCon.OnRosterEnd += new ObjectHandler(xmppCon_OnRosterEnd);
                xmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);

                MessageBox.Show("Login Success");

                this.loginToolStripMenuItem.Enabled = !this.loginToolStripMenuItem.Enabled;
                this.logoutToolStripMenuItem.Enabled = !this.logoutToolStripMenuItem.Enabled;
            }
            else if (d_result==DialogResult.Cancel)
            {
                MessageBox.Show("Login Failed");
            }
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rosterControl.Clear();
            xmppCon.Close();
            this.loginToolStripMenuItem.Enabled = !this.loginToolStripMenuItem.Enabled;
            this.logoutToolStripMenuItem.Enabled = !this.logoutToolStripMenuItem.Enabled;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xmppCon.Close();
            this.Close();
        }
    }
}
